package com.smarttest.portal.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttest.common.beans.DataTablePaginationResult;
import com.smarttest.portal.mapper.StopEventDetailMapper;
import com.smarttest.portal.pojo.StopEventDetailExample;
import com.smarttest.portal.pojo.StopEventDetailExample.Criteria;
import com.smarttest.portal.pojo.StopEventDetailWithBLOBs;
import com.smarttest.portal.service.StopEventDetailService;

@Service
public class StopEventDetailServiceImpl implements StopEventDetailService {

	@Autowired
	StopEventDetailMapper mapper;

	@Override
	public void insert(String imei, String beforeInfo, String afterInfo) {
		// TODO Auto-generated method stub
		StopEventDetailWithBLOBs detail = new StopEventDetailWithBLOBs();
		detail.setImei(imei);
		detail.setBeforeInfo(beforeInfo);
		detail.setAfterInfo(afterInfo);

		mapper.insertSelective(detail);
	}

	@Override
	public int countByTime(Date start, Date end) {
		StopEventDetailExample example = new StopEventDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andCreateTimeBetween(start, end);
		int count = mapper.countByExample(example);
		return count;
	}

	@Override
	public DataTablePaginationResult getStopEventDetailByTime(Date start,
			Date end, int page, int rows, String orderBy) {
		List<StopEventDetailWithBLOBs> list = null;
		
		StopEventDetailExample example = new StopEventDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andCreateTimeBetween(start, end);
		example.setOrderByClause(orderBy);
		
		PageHelper.startPage(page, rows);
		
		list = mapper.selectByExampleWithBLOBs(example);
		
		DataTablePaginationResult result = new DataTablePaginationResult();
		result.setAaData(list);
		
		PageInfo<StopEventDetailWithBLOBs> pageInfo = new PageInfo<>(list);
		
		result.setiTotalDisplayRecords(Long.toString(pageInfo.getTotal()));
		result.setiTotalRecords(Long.toString(pageInfo.getTotal()));
		
		return result;
	}

}
